package com.soup.common.config;

import com.soup.common.constant.CommonConstant;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * 数据库配置
 *
 * @author zhaoyi
 */
@Data
public class DbProperties {

    public static final String DB_CONFIG_PREFIX = "spring.datasource";

    private String url;

    private String driverClassName = "com.mysql.cj.jdbc.Driver";

    private String username = "root";

    private String password = "f1ehmGM9ZUliAVZkmB2eIfbdqST21qLyDeqVJefgwS9GhyHZCRyQFeZMNpxU9bG0dB0DyDY4dyhCeCBLXk5/3A==";

    private Integer initialSize = CommonConstant.NUM_1;

    private Integer minIdle = CommonConstant.NUM_1;

    private Integer maxActive = CommonConstant.NUM_20;

    private Integer maxWait = CommonConstant.NUM_60000;

    private Integer timeBetweenEvictionRunsMillis = CommonConstant.NUM_60000;

    private Integer minEvictableIdleTimeMillis = CommonConstant.NUM_300000;

    private String validationQuery = "select 1 from dual";

    private Boolean testWhileIdle = CommonConstant.TRUE;

    private Boolean testOnBorrow = CommonConstant.FALSE;

    private Boolean testOnReturn = CommonConstant.FALSE;

    private Boolean poolPreparedStatements = CommonConstant.TRUE;

    private Integer maxOpenPreparedStatements = CommonConstant.NUM_20;

    private Integer maxPoolPreparedStatementPerConnectionSize = CommonConstant.NUM_20;

    private String filters = "config,stat,wall,log4j2";

    private String publicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAI2ndXFeQeFBI0HTXmbTrXgs0+4fhPCUkMaHNv55yYBgdK2WgwsFQUWK3hIwp0GQneGc7q3k4sxAQF59Jb+cK5sCAwEAAQ==";

    private String connectionProperties;

    public void setUrl(String url) {
        this.url = url + "?serverTimezone=Asia/Shanghai&characterEncoding=utf8";
    }

    public void setConnectionProperties(String connectionProperties) {
        if (StringUtils.isEmpty(connectionProperties)) {
            this.connectionProperties = "config.decrypt=true;config.decrypt.key=" + this.publicKey;
        } else {
            this.connectionProperties = connectionProperties;
        }
    }
}
